﻿using System;
using System.Xml;
using System.Collections.Generic;

/**
 * 配置数据服务
 */

public class CfgSvc{

    private static CfgSvc instance = null;

    public static CfgSvc Instance{
        get{
            if (instance == null){
                instance = new CfgSvc();
            }

            return instance;
        }
    }

    public void Init(){
        InitGuideCfg();
        InitStrongCfg();
        InitTaskCfg();
        InitMapCfg();
        
        PECommon.Log("CfgSvc Init Done");
    }

    #region 自动引导配置

    private Dictionary<int, GuideCfg> guideDic = new Dictionary<int, GuideCfg>();
    private void InitGuideCfg(){
        var doc = new XmlDocument();
        doc.Load(@"guide.xml");
        XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
        for (var i = 0; i < nodeList.Count; ++i){
            var element = nodeList[i] as XmlElement;
            if (element.GetAttributeNode("ID") == null){
                continue;
            }

            var ID = Convert.ToInt32(element.GetAttributeNode("ID").InnerText);
            GuideCfg mc = new GuideCfg{
                ID = ID
            };
            foreach (XmlElement xmlElement in nodeList[i].ChildNodes){
                switch (xmlElement.Name){
                    case "coin":
                        mc.coin = int.Parse(xmlElement.InnerText);          
                        break;
                    case "exp":
                        mc.exp = int.Parse(xmlElement.InnerText);
                        break;
                }
            }
            guideDic.Add(ID, mc);
        }
        PECommon.Log("GuideCfg Init Done");
    }

    public GuideCfg GetGuideData(int id){
        GuideCfg agc = null;
        if (guideDic.TryGetValue(id, out agc)){
            return agc;
        }
        return null;
    }

    #endregion

    #region 强化升级配置

    private Dictionary<int, Dictionary<int, StrongCfg>> strongDic = new Dictionary<int, Dictionary<int, StrongCfg>>();

    private void InitStrongCfg(){
        var doc = new XmlDocument();
        doc.Load("strong.xml");
        XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
        for (var i = 0; i < nodeList.Count; ++i){
            var element = nodeList[i] as XmlElement;
            if (element.GetAttributeNode("ID") == null){
                continue;
            }

            var ID = Convert.ToInt32(element.GetAttributeNode("ID").InnerText);
            StrongCfg sc = new StrongCfg{
                ID = ID
            };
            foreach (XmlElement xmlElement in nodeList[i].ChildNodes){
                int val = int.Parse(xmlElement.InnerText);
                switch (xmlElement.Name){
                    case "pos":
                        sc.pos = val;
                        break;
                    case "addhp":
                        sc.addhp = val;
                        break;
                    case "addhurt":
                        sc.addhurt = val;
                        break;
                    case "adddef":
                        sc.adddef = val;
                        break;
                    case "minlv":
                        sc.minlv = val;
                        break;
                    case "starlv":
                        sc.starlv = val;
                        break;
                    case "coin":
                        sc.coin = val;
                        break;
                    case "crystal":
                        sc.crystal = val;
                        break;
                }
            }

            Dictionary<int, StrongCfg> dic = null;
            if (strongDic.TryGetValue(sc.pos, out dic)){
                dic.Add(sc.starlv, sc);
            }
            else{
                dic = new Dictionary<int, StrongCfg>();
                dic.Add(sc.starlv, sc);
                strongDic.Add(sc.pos, dic);
            }
        }
        PECommon.Log("StrongCfg Init Done");
    }
    
    public StrongCfg GetStronCfg(int pos, int starlv){
        StrongCfg ret = null;
        Dictionary<int, StrongCfg> dic = null;
        if (strongDic.TryGetValue(pos, out dic)){
            ret = dic[starlv];
        }
        return ret;
    }

    #endregion

    #region 副本

    private Dictionary<int, TaskRewardCfg> taskDic = new Dictionary<int, TaskRewardCfg>();
    private void InitTaskCfg(){
        var doc = new XmlDocument();
        doc.Load(@"taskreward.xml");
        XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
        for (var i = 0; i < nodeList.Count; ++i){
            var element = nodeList[i] as XmlElement;
            if (element.GetAttributeNode("ID") == null){
                continue;
            }

            var ID = Convert.ToInt32(element.GetAttributeNode("ID").InnerText);
            TaskRewardCfg trc = new TaskRewardCfg{
                ID = ID
            };
            foreach (XmlElement xmlElement in nodeList[i].ChildNodes){
                switch (xmlElement.Name){
                    case "count":
                        trc.count = int.Parse(xmlElement.InnerText);          
                        break;
                    case "exp":
                        trc.exp = int.Parse(xmlElement.InnerText);
                        break;
                    case "coin":
                        trc.coin = int.Parse(xmlElement.InnerText);
                        break;
                }
            }
            taskDic.Add(ID, trc);
        }
        PECommon.Log("TaskRewardCfg Init Done");
    }

    public TaskRewardCfg GetTaskCfg(int id){
        TaskRewardCfg trc = null;
        if (taskDic.TryGetValue(id, out trc)){
            return trc;
        }
        return null;
    }


    #endregion
    
    #region 地图配置
    private Dictionary<int, MapCfg> mapDic = new Dictionary<int, MapCfg>();
    private void InitMapCfg() {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"map.xml");

        XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes;

        for (int i = 0; i < nodLst.Count; i++) {
            XmlElement ele = nodLst[i] as XmlElement;

            if (ele.GetAttributeNode("ID") == null) {
                continue;
            }
            int ID = Convert.ToInt32(ele.GetAttributeNode("ID").InnerText);
            MapCfg mc = new MapCfg {
                ID = ID
            };

            foreach (XmlElement e in nodLst[i].ChildNodes) {
                switch (e.Name) {
                    case "power":
                        mc.power = int.Parse(e.InnerText);
                        break;
                }
            }
            mapDic.Add(ID, mc);
        }
        PECommon.Log("MapCfg Init Done.");

    }
    public MapCfg GetMapCfg(int id) {
        MapCfg mc = null;
        if (mapDic.TryGetValue(id, out mc)) {
            return mc;
        }
        return null;
    }
    #endregion
    
}

public class StrongCfg : BaseData<StrongCfg>{
    public int pos;
    public int starlv;
    public int addhp;
    public int addhurt;
    public int adddef;
    public int minlv;
    public int coin;
    public int crystal;
}

public class BaseData<T>{
    public int ID;
}

public class GuideCfg : BaseData<GuideCfg>{
    public int coin;
    public int exp;
}

public class TaskRewardCfg : BaseData<TaskRewardCfg>{
    public int count;
    public int exp;
    public int coin;
}

public class TaskRewardData : BaseData<TaskRewardData>{
    public int prg;
    public bool taked;
}

public class MapCfg : BaseData<MapCfg> {
    public int power;
}
